package com.ruoyi.mng.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mng.domain.HzBusi;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务管理Controller
 *
 * @author chenqiang
 * @date 2023-04-27
 */
@Controller
@RequestMapping("/mng/busi")
public class HzBusiController extends BaseController {
    private String prefix = "mng/busi";

    @Autowired
    private IHzBusiService hzBusiService;

    @RequiresPermissions("mng:busi:view")
    @GetMapping()
    public String busi() {
        return prefix + "/busi";
    }

    /**
     * 查询业务管理列表
     */
    @RequiresPermissions("mng:busi:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(HzBusi hzBusi) {
        startPage();
        List<HzBusi> list = hzBusiService.selectHzBusiList(hzBusi);
        return getDataTable(list);
    }

    /**
     * 导出业务管理列表
     */
    @RequiresPermissions("mng:busi:export")
    @Log(title = "业务管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(HzBusi hzBusi) {
        List<HzBusi> list = hzBusiService.selectHzBusiList(hzBusi);
        ExcelUtil<HzBusi> util = new ExcelUtil<HzBusi>(HzBusi.class);
        return util.exportExcel(list, "业务管理数据");
    }

    /**
     * 新增业务管理
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存业务管理
     */
    @RequiresPermissions("mng:busi:add")
    @Log(title = "业务管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(HzBusi hzBusi) {
        HzBusi oldHzBusi = hzBusiService.selectHzBusiByBusiCodeOrBusiName(hzBusi);
        if (null != oldHzBusi) {
            return AjaxResult.error("已经存在相同业务编码或业务名称");
        }
        return toAjax(hzBusiService.insertHzBusi(hzBusi));
    }

    /**
     * 修改业务管理
     */
    @RequiresPermissions("mng:busi:edit")
    @GetMapping("/edit/{busiId}")
    public String edit(@PathVariable("busiId") Long busiId, ModelMap mmap) {
        HzBusi hzBusi = hzBusiService.selectHzBusiByBusiId(busiId);
        mmap.put("hzBusi", hzBusi);
        return prefix + "/edit";
    }

    /**
     * 修改保存业务管理
     */
    @RequiresPermissions("mng:busi:edit")
    @Log(title = "业务管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(HzBusi hzBusi) {
        HzBusi oldHzBusi = hzBusiService.selectHzBusiByBusiCodeOrBusiName(hzBusi);
        if (null != oldHzBusi) {
            if (!oldHzBusi.getBusiId().equals(hzBusi.getBusiId())) {
                return AjaxResult.error("已经存在相同业务编码或业务名称");
            }
        }
        return toAjax(hzBusiService.updateHzBusi(hzBusi));
    }

    /**
     * 删除业务管理
     */
    @RequiresPermissions("mng:busi:remove")
    @Log(title = "业务管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(hzBusiService.deleteHzBusiByBusiIds(ids));
    }
}
